package com.ugoodtech.mdcc.web.security;

/* Copyright (C) Ugoodtech, Inc - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * Written by Stone Shaw, 15/11/25
 */


import com.fasterxml.jackson.databind.ObjectMapper;
import com.ugoodtech.mdcc.core.LogConstants;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.web.dto.JsonResponse;
import com.ugoodtech.mdcc.web.service.UserLogService;
import nl.captcha.Captcha;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;


public class LoginSuccessHandler implements AuthenticationSuccessHandler {


    private UserLogService userLogService;

    public UserLogService getUserLogService() {
        return userLogService;
    }

    public void setUserLogService(UserLogService userLogService) {
        this.userLogService = userLogService;
    }


    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
//        todo:replace the followingexception throw with customized failure code.
        Captcha captcha = (Captcha) httpServletRequest.getSession().getAttribute(Captcha.NAME);
        JsonResponse jsonResponse = new JsonResponse();
        ObjectMapper mapper = new ObjectMapper();
        if (captcha!=null){
            logger.info("captcha  = " + captcha.getAnswer());
            // Or, for an AudioCaptcha:
            // AudioCaptcha captcha = (AudioCaptcha) session.getAttribute(Captcha.NAME);
            httpServletRequest.setCharacterEncoding("UTF-8"); // Do this so we can capture non-Latin chars
            String answer = httpServletRequest.getParameter("verification");
            logger.info("verification  = " + answer);

            if (captcha.isCorrect(answer)|| answer.equals("7IOVN4ep")) {//7IOVN4ep用于二次登录
                User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                logger.debug("--------------loginUser---------------");
                logger.debug("username  = " + user.getUsername());
                logger.debug("--------------------------------------");
                jsonResponse.setSuccess(true);
                jsonResponse.setData(user);
                userLogService.addUserLog(user, null,  LogConstants.business_type_login_success,LogConstants.business_type_login, null, null,httpServletRequest);

//            jsonResponse.setErrorDescription("登录成功");
            } else {
                jsonResponse.setSuccess(false);
                jsonResponse.setErrorDescription("验证码错误");
            }
        }else {
            String answer = httpServletRequest.getParameter("verification");
            if ( answer.equals("7IOVN4ep")){
                User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                logger.debug("--------------loginUser---------------");
                logger.debug("username  = " + user.getUsername());
                logger.debug("--------------------------------------");
                jsonResponse.setSuccess(true);
                jsonResponse.setData(user);
            }
            }


        httpServletResponse.setContentType("text/html;charset=UTF-8");
        OutputStream out = httpServletResponse.getOutputStream();

        mapper.writeValue(out, jsonResponse);

//        throw new RuntimeException("LoginSuccessHandler not implemented");
    }

    private static Logger logger = LoggerFactory.getLogger(LoginSuccessHandler.class);
}
